import {Message} from 'element-ui'
import store from '@/store'
import router from '@/router'
import * as userApi from "@/api/sys/user";


export const notify = function ({
                                    type = 'info',
                                    message = '',
                                    duration = 2000,
                                    showClose = false,
                                    onClose = function () {
                                    }
                                }) {
    Message({
        type,
        message,
        duration,
        showClose,
        onClose
    })
}

export let checkRouter = () => {
    if (store.state.routerList.length > 0) {
        return true
    } else {
        return false
    }
}


export let initRouter = () => {
    return new Promise(resolve => {
        userApi.getRouters().then(res => {
            store.state.searchMenuList = []
            if (res.code === 200) {
                recursionBuildRouter("layout", res.data)
                store.state.routerList = res.data
                resolve()
            }
        })
    })
}

let recursionBuildRouter = (parentsName, menuData) => {
    menuData.forEach(menu => {
        if (menu.menuType === 0) {
            if (menu.childList) {
                router.addRoute(parentsName, {
                    path: menu.menuName,
                    name: menu.menuName,
                    meta: {
                        id: menu.id,
                        name: menu.menuName,
                        menuType: menu.menuType
                    },
                    component: resolve => require(['@/views/blank'], resolve),
                })
                recursionBuildRouter(menu.menuName, menu.childList)
            }
        } else {
            store.state.searchMenuList.push(menu)
            let names = menu.path.split("/")
            router.addRoute(parentsName, {
                path: menu.path,
                name: names[names.length - 1],
                component: resolve => require(['@/views' + menu.path], resolve),
                meta: {
                    id: menu.id,
                    name: menu.menuName,
                    menuType: menu.menuType
                }
            })
        }
    })
}
export const assignVal = (modelObj, valObj) => {
    if (modelObj instanceof Array) {
        if (valObj) {
            console.log(valObj)
            if (valObj instanceof Array) {
            } else {
                valObj = JSON.parse(valObj);
            }
            valObj.forEach(v => modelObj.push(v))
        }
    } else {
        for (let key in modelObj) {
            if (modelObj[key] instanceof Object) {
                assignVal(modelObj[key], valObj[key])
            } else {
                modelObj[key] = valObj[key]
            }
        }
    }
}

